import type { Rules, RuleItem } from 'async-validator/dist-types/interface';

import View from './view';
import Form from './form';
import { isEmpty, keys } from 'lodash';
import { validatorWrapper, getValidate } from '../utils';

export const rules: Rules = {
  tplType: [
    {
      required: true,
      message: '模板不能为空',
    },
  ],
  bgRadius: [
    {
      type: 'number',
      required: true,
      message: '图片圆角不能为空',
    },
    {
      type: 'number',
      min: 0,
      message: '图片圆角不能小于0',
    },
  ],
  navsData: [
    {
      required: true,
      message: '导航不能为空',
    },
    {
      type: 'array',
      defaultField: {
        validator: validatorWrapper((_, value) => {
          if (!value.title) {
            throw new Error('标题不能为空!');
          }
          if (!value.imageUrl) {
            throw new Error('请上传图片!');
          }
          if (
            isEmpty(value.value) ||
            (keys(value.value).length === 1 && keys(value.value)[0] === 'type')
          ) {
            if (value.pageType === 'goods') {
              throw new Error('请选择商品!');
            }
            if (value.pageType === 'activity') {
              throw new Error('请选择活动!');
            }
            if (value.pageType === 'specifyPage') {
              throw new Error('请选择页面!');
            }
          }
        }),
      } as RuleItem,
    },
  ],
};

export const rulesOptions = {
  title: [
    {
      required: true,
      message: '标题不能空',
    },
  ],
  imageUrl: [
    {
      required: true,
      message: '请上传图片',
    },
  ],
};

export const TPL_TYPE = {
  normal: {
    value: 'normal',
    label: '标准样式',
    image:
      '',
  },
  scroll: {
    value: 'scroll',
    label: '滑动样式',
    image:
      '',
  },
};

export const BG_SET = {
  whithBg: {
    value: 'whithBg',
    label: '有背景',
  },
  noBg: {
    value: 'noBg',
    label: '无背景',
  },
};

export const BOR_STYLE = {
  whithBorder: {
    value: 'whithBorder',
    label: '有边距',
  },
  noBorder: {
    value: 'noBorder',
    label: '无边距',
  },
};

export const IMGERROR =
  '';

export const navsDataItem = {
  title: '',
  imageUrl: '',
  pageType: 'goods',
  value: {
    title: '',
    id: '',
    page: '',
    type: 'goods',
  },
};

const initialValues = {
  tplType: TPL_TYPE.normal.value, // 模板选择
  bgSetting: BG_SET.whithBg.value, // 背景设置
  bgRadius: 0, // 背景圆角
  borderStyle: BOR_STYLE.noBorder.value, // 边距设置
  navsData: [navsDataItem], // 导航数组-数据
};

export type NavigationValue = typeof initialValues;

export default {
  type: 'Navigation',
  title: '导航',
  logo: 'https://saascdn.yumchina.com/crmyumc/768479403501420544/member/202308211335262279.jpg',
  initialValues,
  view: View,
  form: Form,
  limit: 1,
  validate: getValidate(rules),
};
